package org.example.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.example.core.enums.BizErrorEnum;
import org.example.core.web.PageResult;
import org.example.core.web.Result;
import org.example.sys.entity.dto.UserDTO;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.util.List;

/**
 * Controller基类
 * @author Calvin
 */
@Slf4j
public class ApiController {

    /**
     * 获取当前登录的user
     * @return User
     */
    public UserDTO getLoginUser() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null) {
                Object object = authentication.getPrincipal();
                if (object instanceof UserDTO) {
                    return (UserDTO) object;
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 获取当前登录的userId
     *
     * @return userId
     */
    public Long getLoginUserId() {
        UserDTO loginUser = getLoginUser();
        return loginUser == null ? null : loginUser.getUserId();
    }

    /**
     * 返回成功
     * @return Result
     */
    public Result<?> success() {
        return new Result<>(BizErrorEnum.RESULT_OK.getCode(), BizErrorEnum.RESULT_OK.getDesc());
    }

    /**
     * 返回成功
     * @param data 返回数据
     * @return Result
     */
    public <T> Result<T> success(T data) {
        return new Result<>(BizErrorEnum.RESULT_OK.getCode(), BizErrorEnum.RESULT_OK.getDesc(), data);
    }

    /**
     * 返回分页查询数据
     * @param list  当前页数据
     * @param count 总数量
     * @return Result
     */
    public <T> Result<PageResult<T>> success(List<T> list, Long count) {
        return success(new PageResult<>(list, count));
    }

    /**
     * 返回分页查询数据
     *
     * @param msg  当前页数据
     * @param data 总数量
     * @return Result
     */
    public <T> Result<T> success(String msg, T data) {
        return new Result<>(BizErrorEnum.RESULT_OK.getCode(), msg, data);
    }

    /**
     * 返回分页查询数据
     *
     * @param iPage IPage
     * @return Result
     */
    public <T> Result<PageResult<T>> success(IPage<T> iPage) {
        return success(iPage.getRecords(), iPage.getTotal());
    }

    /**
     * 返回失败
     *
     * @return Result
     */
    public Result<?> fail() {
        return new Result<>(BizErrorEnum.RESULT_ERROR.getCode(), BizErrorEnum.RESULT_ERROR.getDesc());
    }

    /**
     * 返回失败
     *
     * @param message 状态信息
     * @return Result
     */
    public Result<?> fail(String message) {
        return new Result<>(BizErrorEnum.RESULT_ERROR.getCode(), message);
    }


    /**
     * 返回失败
     *
     * @param message 状态信息
     * @param data    返回数据
     * @return Result
     */
    public <T> Result<T> fail(String message, T data) {
        return new Result<>(BizErrorEnum.RESULT_ERROR.getCode(), message, data);
    }

    /**
     * 返回失败
     *
     * @param message 状态信息
     * @param data    返回数据
     * @return Result
     */
    public <T> Result<T> fail(Integer code,String message, T data) {
        return new Result<>(code, message, data);
    }

    /**
     * 请求参数的空字符串转为null
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

}
